#ifndef DOWNLOADER_H
#define DOWNLOADER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkReply>
#include <QFile>
#include <QTime>
#include <QProgressBar>
#include "priorityqueue.h"
#include "downloadtask.h"

class Downloader : public QObject
{
    Q_OBJECT
public:
    explicit Downloader(QObject *parent = 0);

    void append(DownloadTask *task);
    QString saveFileName(const QString &rename, const QString &savePath);

signals:
    void finished();
    void progressChanged(QString fileName, int progress);
    
private slots:
    void startNextDownload();
    void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
    void downloadFinished();
    void downloadReadyRead();

private:
    QNetworkAccessManager manager;
    PriorityQueue downloadQueue;
    QNetworkReply *currentDownload;
    QString currentFilePath;
    QFile output;
    QTime downloadTime;
    QProgressBar *progressBar;

    int downloadedCount;
    int totalCount;

    
};

#endif // DOWNLOADER_H
